package se137.hrbust.hlgshopping137.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import se137.hrbust.hlgshopping137.service.UserService137;
import se137.hrbust.hlgshopping137.utils.JwtUtil137;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class AuthInterceptor implements HandlerInterceptor {
    @Lazy
    @Autowired
    UserService137 userService;
    @Autowired
    JwtUtil137 jwtUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7);
//          if(userService.validateToken(token)){
//              return true;
//          }
        }
            response.setStatus(401);
            response.getWriter().write("{\"code\":401,\"message\":\"invalid\"}");
//      return false;
            return true;

        }
    }

